All code, unless otherwise noted, is courtesy of Chris Gross.